package com.example.lanouhn.bantangapp.activities.activities;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.contants.Notifination_ChatEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author way
 */
public  class Msg_Notifination_Activity extends Activity {
    private TextView Lmessage;
    private EditText contentEditText = null;
    private ListView chatListView = null;
    private List<Notifination_ChatEntity> chatList = null;
    private ChatAdapter chatAdapter = null;
    boolean jianghua;
    private ImageView back;//返回

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.msg_leaingmessage_activity);

        contentEditText = (EditText) this.findViewById(R.id.et_content);

        Lmessage = (TextView) findViewById(R.id.Lmessage);
        chatListView = (ListView) this.findViewById(R.id.listview);
        back = (ImageView) findViewById(R.id.back);
        initListener();


        chatList = new ArrayList<Notifination_ChatEntity>();
        Notifination_ChatEntity notifinationChatEntity = null;
        for (int i = 0; i < 2; i++) {
            notifinationChatEntity = new Notifination_ChatEntity();
            if (i % 2 == 0) {
                notifinationChatEntity.setComeMsg(true);
                notifinationChatEntity.setContent(getResources().getString(R.string.message));

                //                获取当前时间
                SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
                String timeStr = dateFormat.format(new Date());
                notifinationChatEntity.setChatTime(timeStr);
            }else {
                notifinationChatEntity.setComeMsg(false);
                notifinationChatEntity.setContent(getResources().getString(R.string.hello));
                //                获取当前时间
                SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
                String timeStr = dateFormat.format(new Date());
                notifinationChatEntity.setChatTime(timeStr);
            }
            chatList.add(notifinationChatEntity);
        }

        chatAdapter = new ChatAdapter(this,chatList);
        chatListView.setAdapter(chatAdapter);

        Lmessage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!contentEditText.getText().toString().equals("")) {
                    //发送消息
                    send();
                }else {
                    Toast.makeText(Msg_Notifination_Activity.this, R.string.please, Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    //监听事件
    private void initListener() {
        Lmessage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!jianghua) {
                    Lmessage.setVisibility(View.VISIBLE);
                }else {
                    Lmessage.setVisibility(View.INVISIBLE);
                }
            }
        });

        Lmessage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void send(){
        Notifination_ChatEntity notifinationChatEntity = new Notifination_ChatEntity();
        notifinationChatEntity.setChatTime("2012-09-20 15:16:34");
        notifinationChatEntity.setContent(contentEditText.getText().toString());
        notifinationChatEntity.setComeMsg(false);
        chatList.add(notifinationChatEntity);
        chatAdapter.notifyDataSetChanged();
        chatListView.setSelection(chatList.size() - 1);
        contentEditText.setText("");
    }




    private  class ChatAdapter extends BaseAdapter {
        private Context context = null;
        private List<Notifination_ChatEntity> chatList = null;
        private LayoutInflater inflater = null;
        private int COME_MSG = 0;
        private int TO_MSG = 1;

        public ChatAdapter(Context context, List<Notifination_ChatEntity> chatList){
            this.context = context;
            this.chatList = chatList;
            inflater = LayoutInflater.from(this.context);
        }

        @Override
        public int getCount() {
            return chatList.size();
        }

        @Override
        public Object getItem(int position) {
            return chatList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemViewType(int position) {
            // 区别两种view的类型，标注两个不同的变量来分别表示各自的类型
            Notifination_ChatEntity entity = chatList.get(position);
            if (entity.isComeMsg())
            {
                return COME_MSG;
            }else{
                return TO_MSG;
            }
        }

        @Override
        public int getViewTypeCount() {
            // 这个方法默认返回1，如果希望listview的item都是一样的就返回1，我们这里有两种风格，返回2
            return 2;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ChatHolder chatHolder = null;
            if (convertView == null) {
                chatHolder = new ChatHolder();
                if (chatList.get(position).isComeMsg()) {
                    convertView = inflater.inflate(R.layout.msg_viewpager_listview_item_activity_recyclerview_sendr_item, null);
                }else {
                    convertView = inflater.inflate(R.layout.msg_viewpager_listview_item_activity_recyclerview_recicr_item, null);
                }
                chatHolder.timeTextView = (TextView) convertView.findViewById(R.id.tv_time);
                chatHolder.contentTextView = (TextView) convertView.findViewById(R.id.tv_content);
                chatHolder.userImageView = (ImageView) convertView.findViewById(R.id.iv_user_image);
                convertView.setTag(chatHolder);
            }else {
                chatHolder = (ChatHolder)convertView.getTag();
            }

            chatHolder.timeTextView.setText(chatList.get(position).getChatTime());
            chatHolder.contentTextView.setText(chatList.get(position).getContent());
            chatHolder.userImageView.setImageResource(chatList.get(position).getUserImage());

            return convertView;
        }

        private class ChatHolder{
            private TextView timeTextView;
            private ImageView userImageView;
            private TextView contentTextView;
        }

    }
}